// -------------------------------------------------------------------------------------------------
// Copyright 2024 Kearn Chen, kearn.chen@aliyun.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------
// Description :
//             1. Core Top Wrapper
// -------------------------------------------------------------------------------------------------

module k0a_core_top (
    input  wire         core_clk    ,
    input  wire         core_rstn   ,

    output wire         bus_avalid  ,
    input  wire         bus_aready  ,
    output wire         bus_write   ,
    output wire [17:0]  bus_addr    ,
    output wire [3:0]   bus_strb    ,
    output wire [31:0]  bus_wdata   ,
    input  wire         bus_rvalid  ,
    output wire         bus_rready  ,
    input  wire [31:0]  bus_rdata   ,

    input  wire [15:0]  irq_lines
);

wire        idu2bmu_pc_set    ;
wire [17:0] idu2bmu_pc_new    ;
wire        bmu2idu_pc_ack    ;

wire        idu2bmu_ls_req    ;
wire        idu2bmu_ls_cmd    ;
wire [1:0]  idu2bmu_ls_size   ;
wire [19:0] idu2bmu_ls_addr   ;
wire [31:0] idu2bmu_ls_wdata  ;
wire [31:0] bmu2idu_ls_rdata  ;

wire        bmu2idu_valid     ;
wire [31:0] bmu2idu_instr     ;
wire        idu2bmu_resp      ;
wire        idu2bmu_wfi       ;
wire [17:0] bmu2idu_pc_cur    ;
wire [17:0] bmu2idu_pc_nxt    ;

wire        idu2gpr_we        ;
wire [3:0]  idu2gpr_waddr     ;
wire [31:0] idu2gpr_wdata     ;
wire [3:0]  idu2gpr_raddr1    ;
wire [3:0]  idu2gpr_raddr2    ;
wire [31:0] gpr2idu_rdata1    ;
wire [31:0] gpr2idu_rdata2    ;

wire        idu2csr_we        ;
wire [11:0] idu2csr_addr      ;
wire [31:0] idu2csr_wdata     ;
wire [31:0] csr2idu_rdata     ;
wire [17:0] csr2idu_mepc      ;
wire [17:0] csr2idu_mtvec     ;
wire        idu2csr_mepc_set  ;
wire [17:0] idu2csr_mepc_nxt  ;

wire        csr2cic_gie       ;
wire [15:0] csr2cic_mie       ;
wire [15:0] csr2cic_mip       ;
wire [15:0] cic2csr_irq       ;
wire [4:0]  cic2csr_mcause    ;

wire [3:0]  idu2alu_op        ;
wire [31:0] idu2alu_rs1       ;
wire [31:0] idu2alu_rs2       ;
wire [31:0] alu2idu_res       ;
wire        alu2idu_cmp       ;
wire [19:0] idu2alu_addr1     ;
wire [19:0] idu2alu_addr2     ;
wire [19:0] alu2idu_addro     ;

wire        cic2idu_int_req   ;
wire        idu2cic_int_ack   ;
wire        idu2cic_int_mret  ;

k0a_core_bmu u_core_bmu (
    .core_clk           (core_clk           ),
    .core_rstn          (core_rstn          ),
    .bus_avalid         (bus_avalid         ),
    .bus_aready         (bus_aready         ),
    .bus_write          (bus_write          ),
    .bus_addr           (bus_addr           ),
    .bus_strb           (bus_strb           ),
    .bus_wdata          (bus_wdata          ),
    .bus_rvalid         (bus_rvalid         ),
    .bus_rready         (bus_rready         ),
    .bus_rdata          (bus_rdata          ),
    .idu2bmu_pc_set     (idu2bmu_pc_set     ),
    .idu2bmu_pc_new     (idu2bmu_pc_new     ),
    .bmu2idu_pc_ack     (bmu2idu_pc_ack     ),
    .idu2bmu_ls_req     (idu2bmu_ls_req     ),
    .idu2bmu_ls_cmd     (idu2bmu_ls_cmd     ),
    .idu2bmu_ls_size    (idu2bmu_ls_size    ),
    .idu2bmu_ls_addr    (idu2bmu_ls_addr    ),
    .idu2bmu_ls_wdata   (idu2bmu_ls_wdata   ),
    .bmu2idu_ls_rdata   (bmu2idu_ls_rdata   ),
    .bmu2idu_valid      (bmu2idu_valid      ),
    .bmu2idu_instr      (bmu2idu_instr      ),
    .bmu2idu_pc_cur     (bmu2idu_pc_cur     ),
    .bmu2idu_pc_nxt     (bmu2idu_pc_nxt     )
);

k0a_core_idu u_core_idu (
    .bmu2idu_valid      (bmu2idu_valid      ),
    .bmu2idu_instr      (bmu2idu_instr      ),
    .bmu2idu_pc_cur     (bmu2idu_pc_cur     ),
    .bmu2idu_pc_nxt     (bmu2idu_pc_nxt     ),
    .idu2bmu_pc_set     (idu2bmu_pc_set     ),
    .bmu2idu_pc_ack     (bmu2idu_pc_ack     ),
    .idu2bmu_pc_new     (idu2bmu_pc_new     ),
    .idu2bmu_ls_req     (idu2bmu_ls_req     ),
    .idu2bmu_ls_cmd     (idu2bmu_ls_cmd     ),
    .idu2bmu_ls_size    (idu2bmu_ls_size    ),
    .idu2bmu_ls_addr    (idu2bmu_ls_addr    ),
    .idu2bmu_ls_wdata   (idu2bmu_ls_wdata   ),
    .bmu2idu_ls_rdata   (bmu2idu_ls_rdata   ),
    .idu2gpr_we         (idu2gpr_we         ),
    .idu2gpr_waddr      (idu2gpr_waddr      ),
    .idu2gpr_wdata      (idu2gpr_wdata      ),
    .idu2gpr_raddr1     (idu2gpr_raddr1     ),
    .idu2gpr_raddr2     (idu2gpr_raddr2     ),
    .gpr2idu_rdata1     (gpr2idu_rdata1     ),
    .gpr2idu_rdata2     (gpr2idu_rdata2     ),
    .idu2csr_we         (idu2csr_we         ),
    .idu2csr_addr       (idu2csr_addr       ),
    .idu2csr_wdata      (idu2csr_wdata      ),
    .csr2idu_rdata      (csr2idu_rdata      ),
    .csr2idu_mepc       (csr2idu_mepc       ),
    .csr2idu_mtvec      (csr2idu_mtvec      ),
    .idu2csr_mepc_set   (idu2csr_mepc_set   ),
    .idu2csr_mepc_nxt   (idu2csr_mepc_nxt   ),
    .idu2alu_op         (idu2alu_op         ),
    .idu2alu_rs1        (idu2alu_rs1        ),
    .idu2alu_rs2        (idu2alu_rs2        ),
    .alu2idu_res        (alu2idu_res        ),
    .alu2idu_cmp        (alu2idu_cmp        ),
    .idu2alu_addr1      (idu2alu_addr1      ),
    .idu2alu_addr2      (idu2alu_addr2      ),
    .alu2idu_addro      (alu2idu_addro      ),
    .cic2idu_int_req    (cic2idu_int_req    ),
    .idu2cic_int_ack    (idu2cic_int_ack    ),
    .idu2cic_int_mret   (idu2cic_int_mret   )
);

k0a_core_gpr u_core_gpr (
    .core_clk           (core_clk           ),
    .idu2gpr_we         (idu2gpr_we         ),
    .idu2gpr_waddr      (idu2gpr_waddr      ),
    .idu2gpr_wdata      (idu2gpr_wdata      ),
    .idu2gpr_raddr1     (idu2gpr_raddr1     ),
    .gpr2idu_rdata1     (gpr2idu_rdata1     ),
    .idu2gpr_raddr2     (idu2gpr_raddr2     ),
    .gpr2idu_rdata2     (gpr2idu_rdata2     )
);

k0a_core_csr u_core_csr (
    .core_clk           (core_clk           ),
    .core_rstn          (core_rstn          ),
    .idu2csr_we         (idu2csr_we         ),
    .idu2csr_addr       (idu2csr_addr       ),
    .idu2csr_wdata      (idu2csr_wdata      ),
    .csr2idu_rdata      (csr2idu_rdata      ),
    .csr2cic_gie        (csr2cic_gie        ),
    .csr2cic_mie        (csr2cic_mie        ),
    .csr2cic_mip        (csr2cic_mip        ),
    .cic2csr_irq        (cic2csr_irq        ),
    .cic2csr_mcause     (cic2csr_mcause     ),
    .csr2idu_mepc       (csr2idu_mepc       ),
    .csr2idu_mtvec      (csr2idu_mtvec      ),
    .idu2csr_mepc_set   (idu2csr_mepc_set   ),
    .idu2csr_mepc_nxt   (idu2csr_mepc_nxt   )
);

k0a_core_cic u_core_cic (
    .core_clk           (core_clk           ),
    .core_rstn          (core_rstn          ),
    .irq_lines          (irq_lines          ),
    .csr2cic_gie        (csr2cic_gie        ),
    .csr2cic_mie        (csr2cic_mie        ),
    .csr2cic_mip        (csr2cic_mip        ),
    .cic2csr_irq        (cic2csr_irq        ),
    .cic2csr_mcause     (cic2csr_mcause     ),
    .cic2idu_int_req    (cic2idu_int_req    ),
    .idu2cic_int_ack    (idu2cic_int_ack    ),
    .idu2cic_int_mret   (idu2cic_int_mret   )
);

k0a_core_alu u_core_alu (
    .idu2alu_op         (idu2alu_op         ),
    .idu2alu_rs1        (idu2alu_rs1        ),
    .idu2alu_rs2        (idu2alu_rs2        ),
    .alu2idu_res        (alu2idu_res        ),
    .alu2idu_cmp        (alu2idu_cmp        ),
    .idu2alu_addr1      (idu2alu_addr1      ),
    .idu2alu_addr2      (idu2alu_addr2      ),
    .alu2idu_addro      (alu2idu_addro      )
);

endmodule
